﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Back.Xinkui.Entity.Advert;
using Back.Xinkui.Common;
using DataFrameworkAccess;
using CommonFramework.Utility;

namespace Back.Xinkui.DataAccess.Advert
{
    public class AdvertDataAccess
    {
        /// <summary>
        /// 获取广告位列表
        /// </summary>
        /// <returns></returns>
        public static List<AdvertSpaceEntity> GetAdvertSpaceList(string where, Pager page)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Query.GetAdvertSpaceList");
            cmd.SetParameterValue("@PageIndex", page.PageIndex);
            cmd.SetParameterValue("@WHERE", where);
            cmd.SetParameterValue("@PageSize", page.PageSize);
            var list = cmd.ExecuteEntityList<AdvertSpaceEntity>();

            page.TotalCount = cmd.GetParameterValue("@AllCount").TryType<int>(0);
            return list;
        }

        /// <summary>
        /// 获取广告位信息
        /// </summary>
        public static AdvertSpaceEntity GetAdvertSpaceByID(int id)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Query.GetAdvertSpaceByID");
            cmd.SetParameterValue("@ID", id);
            return cmd.ExecuteEntity<AdvertSpaceEntity>();
        }

        /// <summary>
        /// 新增广告位
        /// </summary>
        /// <param name="entity"></param>
        public static void CreateAdvertSpace(AdvertSpaceEntity entity)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Action.CreateAdvertSpace");
            cmd.SetParameterValue("@BoardID", entity.BoardID);
            cmd.SetParameterValue("@SpaceNo", entity.SpaceNo);
            cmd.SetParameterValue("@SpaceName", entity.SpaceName);
            cmd.SetParameterValue("@MaxCount", entity.MaxCount);
            cmd.SetParameterValue("@SourceType", entity.SourceType);
            cmd.SetParameterValue("@SpaceType", entity.SpaceType);
            cmd.SetParameterValue("@Width", entity.Width);
            cmd.SetParameterValue("@Height", entity.Height);
            cmd.SetParameterValue("@Url", entity.Url);
            cmd.SetParameterValue("@Code", entity.Code);
            cmd.SetParameterValue("@ImageSrc", entity.ImageSrc);
            cmd.SetParameterValue("@Description", entity.Description);
            cmd.SetParameterValue("@CreateUser", entity.CreateUser);
            cmd.SetParameterValue("@SizeType", entity.SizeType);
            cmd.SetParameterValue("@BWidth", entity.BWidth);
            cmd.SetParameterValue("@BHeight", entity.BHeight);
            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 更新广告位
        /// </summary>
        /// <param name="entity"></param>
        public static void UpdateAdvertSpace(AdvertSpaceEntity entity)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Action.UpdateAdvertSpace");
            cmd.SetParameterValue("@ID", entity.ID);
            cmd.SetParameterValue("@SpaceNo", entity.SpaceNo);
            cmd.SetParameterValue("@SpaceName", entity.SpaceName);
            cmd.SetParameterValue("@MaxCount", entity.MaxCount);
            cmd.SetParameterValue("@SourceType", entity.SourceType);
            cmd.SetParameterValue("@SpaceType", entity.SpaceType);
            cmd.SetParameterValue("@Width", entity.Width);
            cmd.SetParameterValue("@Height", entity.Height);
            cmd.SetParameterValue("@Url", entity.Url);
            cmd.SetParameterValue("@Code", entity.Code);
            cmd.SetParameterValue("@ImageSrc", entity.ImageSrc);
            cmd.SetParameterValue("@Description", entity.Description);
            cmd.SetParameterValue("@UpdateUser", entity.UpdateUser);
            cmd.SetParameterValue("@SizeType", entity.SizeType);
            cmd.SetParameterValue("@BWidth", entity.BWidth);
            cmd.SetParameterValue("@BHeight", entity.BHeight);
            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 删除广告位
        /// </summary>
        /// <param name="entity"></param>
        public static void DelAdvertSpace(int ID)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Action.DelAdvertSpace");
            cmd.SetParameterValue("@ID", ID);
            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 获取广告列表
        /// </summary>
        /// <returns></returns>
        //public static List<AdvertEntity> GetAdvertList(string where, Pager page)
        //{
        //    DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Query.GetAdvertList");
        //    cmd.SetParameterValue("@PageIndex", page.PageIndex);
        //    cmd.SetParameterValue("@WHERE", where);
        //    cmd.SetParameterValue("@PageSize", page.PageSize);
        //    var list = cmd.ExecuteEntityList<AdvertEntity>();

        //    page.TotalCount = cmd.GetParameterValue("@AllCount").TryType<int>(0);
        //    return list;
        //}

        /// <summary>
        /// 获取广告列表
        /// </summary>
        /// <returns></returns>
        public static List<AdvertEntity> GetAdvertList(AdvertEntity sEntity, Pager page)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Query.GetAdvertList");
            cmd.SetParameterValue("@SpaceID", sEntity.SpaceID);
            cmd.SetParameterValue("@AdvertName", sEntity.AdvertName);
            cmd.SetParameterValue("@SpaceName", sEntity.SpaceName);
            cmd.SetParameterValue("@KeyWords", sEntity.KeyWords);
            cmd.SetParameterValue("@AdvertType", sEntity.AdvertType);
            cmd.SetParameterValue("@BeginDate", sEntity.BeginDate);
            cmd.SetParameterValue("@EndDate", sEntity.EndDate);
            cmd.SetParameterValue("@Status", sEntity.Status);
            cmd.SetParameterValue("@StartRowID", (page.PageIndex - 1) * page.PageSize + 1);
            cmd.SetParameterValue("@EndRowID", page.PageIndex * page.PageSize);
            var list = cmd.ExecuteEntityList<AdvertEntity>();

            page.TotalCount = cmd.GetParameterValue("@COUNT").TryType<int>(0);
            return list;
        }

        /// <summary>
        /// 获取广告信息
        /// </summary>
        public static AdvertEntity GetAdvertByID(int id)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Query.GetAdvertByID");
            cmd.SetParameterValue("@ID", id);
            return cmd.ExecuteEntity<AdvertEntity>();
        }

        /// <summary>
        /// 新增广告
        /// </summary>
        public static void CreateAdvert(AdvertEntity entity)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Action.CreateAdvert");
            cmd.SetParameterValue("@SpaceID", entity.SpaceID);
            cmd.SetParameterValue("@AdvertNo", entity.AdvertNo);
            cmd.SetParameterValue("@AdvertName", entity.AdvertName);
            cmd.SetParameterValue("@AdvertType", entity.AdvertType);
            cmd.SetParameterValue("@Alt", entity.Alt);
            cmd.SetParameterValue("@EndTime", entity.EndTime);
            cmd.SetParameterValue("@BeginTime", entity.BeginTime);
            cmd.SetParameterValue("@Description", entity.Description);
            cmd.SetParameterValue("@KeyWords", entity.KeyWords);
            cmd.SetParameterValue("@AdvertPath", entity.AdvertPath);
            cmd.SetParameterValue("@BAdvertPath", entity.BAdvertPath);
            cmd.SetParameterValue("@Text", entity.Text);
            cmd.SetParameterValue("@Url", entity.Url);
            cmd.SetParameterValue("@CreateUser", entity.CreateUser);
            cmd.SetParameterValue("@Status", entity.Status);
            cmd.SetParameterValue("@CompanyCrmID", entity.CompanyCrmID);
            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 更新广告
        /// </summary>
        public static void UpdateAdvert(AdvertEntity entity)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Action.UpdateAdvert");
            cmd.SetParameterValue("@ID", entity.ID);
            cmd.SetParameterValue("@AdvertNo", entity.AdvertNo);
            cmd.SetParameterValue("@AdvertName", entity.AdvertName);
            cmd.SetParameterValue("@AdvertType", entity.AdvertType);
            cmd.SetParameterValue("@Alt", entity.Alt);
            cmd.SetParameterValue("@EndTime", entity.EndTime);
            cmd.SetParameterValue("@BeginTime", entity.BeginTime);
            cmd.SetParameterValue("@Description", entity.Description);
            cmd.SetParameterValue("@KeyWords", entity.KeyWords);
            cmd.SetParameterValue("@AdvertPath", entity.AdvertPath);
            cmd.SetParameterValue("@BAdvertPath", entity.BAdvertPath);
            cmd.SetParameterValue("@Text", entity.Text);
            cmd.SetParameterValue("@Url", entity.Url);
            cmd.SetParameterValue("@UpdateUser", entity.UpdateUser);
            cmd.SetParameterValue("@Status", entity.Status);
            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 删除广告
        /// </summary>
        public static void DelAdvert(int ID)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Action.DelAdvert");
            cmd.SetParameterValue("@ID", ID);
            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 排序
        /// </summary>
        public static void ChangeOrder(int ID, int Step)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Action.ChangeOrder");
            cmd.SetParameterValue("@ID", ID);
            cmd.SetParameterValue("@Step", Step);
            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 更改状态
        /// </summary>
        public static void ChangeStatus(string Adverts, int Status)
        {
            DataCommand cmd = DataCommandManager.GetDataCommand("Advert.Action.ChangeStatus");
            cmd.SetParameterValue("@Adverts", Adverts);
            cmd.SetParameterValue("@Status", Status);
            cmd.ExecuteNonQuery();
        }

        public static string GetClickViewByDate(int id, string start, string end)
        {
            DataCommand command = DataCommandManager.GetDataCommand("Advert.Query.GetClickViewByDate");
            command.SetParameterValue("@ID", id);
            command.SetParameterValue("@startDate", start);
            command.SetParameterValue("@endDate", end);
            return command.ExecuteScalar<string>();
        }

        public static string GetClickIpByDate(int id, string start, string end)
        {
            DataCommand command = DataCommandManager.GetDataCommand("Advert.Query.GetClickIpByDate");
            command.SetParameterValue("@ID", id);
            command.SetParameterValue("@startDate", start);
            command.SetParameterValue("@endDate", end);
            return command.ExecuteScalar<int>().ToString();
        }
    }
}
